<template>
  <el-dialog :title="isEdit?'编辑':'新增'" :visible.sync="dialogVisible" width="20%">
    <el-form ref="formItem" :model="formItem" label-width="80px" size="small" :rules="rules">
      <el-row>
        <el-col :span="24">
          <el-form-item label="产品分类" prop="prodType">
            <el-select v-model="formItem.prodType" placeholder="请选择">
              <el-option label="散装" value="A" />
              <!-- <el-option label="包装" value="B" /> -->
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="产品编号" prop="prodId">
            <el-input v-model="formItem.prodId" placeholder="例如0001">
              <template slot="prepend">{{formItem.prodType}}</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="产品名称" prop="prodName">
            <el-input v-model="formItem.prodName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="型号规格" prop="model">
            <el-input v-model="formItem.model"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="基准单价" prop="unitPrice">
            <el-input v-model="formItem.unitPrice" placeholder="保留两位小数"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="单位" prop="unit">
            <el-select v-model="formItem.unit" placeholder="请选择">
              <el-option label="吨" :value="1" />
              <el-option label="公斤" :value="2" />
              <el-option label="包" :value="3" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit('formItem')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { modalFormMixin } from '@/utils/mixin'
import { addProd, updateProd } from '@/api/prod'
export default {
  data () {
    return {
      isEdit: false,
      dialogVisible: false,
      formItem: {
        id: '',
        prodId: '',
        prodType: '',
        prodName: '',
        model: '',
        unitPrice: '',
        unit: ''
      },
      rules: {
        prodId: [{ required: true, message: '请输入产品编号', trigger: 'blur' }],
        prodName: [{ required: true, message: '请输入产品名称', trigger: 'blur' }],
        model: [{ required: true, message: '请输入', trigger: 'blur' }],
        unitPrice: [{ required: true, message: '请输入', trigger: 'blur' }],
        unit: [{ required: true, message: '请输入', trigger: 'blur' }],
        prodType: [{ required: true, message: '请选择', trigger: 'blur' }]
      }
    }
  },
  mixins: [modalFormMixin],
  methods: {
    handleSubmit (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = this.isEdit ? await updateProd(this.formItem) : await addProd(this.formItem)
          if (res.code === 200) {
            this.$emit('ok')
            this.dialogVisible = false
          } else {
            this.$message.error(res.msg)
          }
        }
      })
    }
  }
}
</script>

<style>
</style>